﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dack_Web2.DTO;
using Dack_Web2.BUS;
namespace Dack_Web2
{
    public partial class DangKy : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (CurrentContext.IsLogged)
            {
                string url = ViewState["RefUrl"] == null ? "~/Default.aspx" :
                ViewState["RefUrl"].ToString();
                Response.Redirect(url);

            }
            if (!IsPostBack)
            {
                drThang.Items.Clear();
                for (int i = 1; i < 13; i++)
                {
                    drThang.Items.Add(i.ToString());
                }
                drThang.SelectedIndex = 1;
                drNam.Items.Clear();
                for (int i = 1900; i < 2800; i++)
                {
                    drNam.Items.Add(i.ToString());
                }
                drNam.SelectedIndex = 92;
                drThang_SelectedIndexChanged(null, null);
            }

        }

        protected void cbCongTy_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCongTy.Checked == true)
            {
                lbTen.Text = "Tên công ty :";
                PanelNgaySinh.Visible = false;
            }
            else
            {
                lbTen.Text = "Tên khách hàng";
                PanelNgaySinh.Visible = true;
            }
        }


        protected void btnDangKy_Click(object sender, EventArgs e)
        {
            if (chkQuyChe.Checked)
            {
                if (chkCongTy.Checked)
                {
                    CongTyDTO CongTy = new CongTyDTO();
                    CongTy.DiaChi = txtDiaChi.Text;
                    CongTy.SoDienThoai = txtSDT.Text;
                    CongTy.TenCongTy = txtTen.Text;
                    CongTy.TenDangNhap = txtTenDangNhap.Text;
                    CongTyBUS.Insert(CongTy);
                }
                else
                {
                    KhachHangDTO KhachHang = new KhachHangDTO();
                    KhachHang.DiaChi = txtDiaChi.Text;
                    DateTime A = new DateTime(int.Parse(drNam.SelectedItem.ToString()),
                                            int.Parse(drThang.SelectedItem.ToString()),
                                            int.Parse(drNgay.SelectedItem.ToString()));
                    KhachHang.NgaySinh = A;
                    KhachHang.SoDienThoai = txtSDT.Text;
                    KhachHang.TenDangNhap = txtTenDangNhap.Text;
                    KhachHang.TenKhachHang = txtTen.Text;
                    KhachHang.GioiTinh = true;
                    if (rbNu.Checked)
                    {
                        KhachHang.GioiTinh = true;
                    }
                    KhachHangBUS INS = new KhachHangBUS();
                    INS.Insert(KhachHang);
                }
                UserDTO UNew = new UserDTO();
                UNew.TenDangNhap = txtTenDangNhap.Text;
                UNew.MatKhau = txtMatKhau.Text;
                UNew.NgayDK = DateTime.Now.Date;
                UserBUS.Insert(UNew);
                Response.Redirect("DangKiThanhCong.aspx");
            }
            else
            {
                lbQuyChe.Text = "Bạn vui lòng đọc kỹ quy chế, nếu đọc rồi vui lòng check vào ô trống !!!";
            }
        }

        protected void drThang_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drThang.SelectedItem.Text == "2")
            {
                drNgay.Items.Clear();
                for (int i = 1; i < 30; i++)
                {
                    drNgay.Items.Add(i.ToString());
                }
            }
            else
            {
                drNgay.Items.Clear();
                for (int i = 1; i < 32; i++)
                {
                    drNgay.Items.Add(i.ToString());
                }
            }
        }
    }
}